package com.gis3c.datacenter.web.controller;

import com.gis3c.datacenter.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hukekuan on 2018/3/28.
 */
@RestController
@RequestMapping(value = "/api/datacenter")
public class DataCenterController {
    @Autowired
    private EnterpriseService enterpriseService;


    @RequestMapping(value = "enterpriselist")
    public String findEnterpriseList(
            @RequestParam(value = "areacode",required = false) String areaCode,
            @RequestParam(value = "pstype",required = false) String psType,
            @RequestParam(value = "enterprisename",required = false) String enterpriseName,
            @RequestParam(value = "controllevels",required = false) String controlLevels){
        Map<String,Object> queryParams = new HashMap<>();
        if(areaCode != null){
            queryParams.put("areaCode",areaCode);
        }
        if(psType != null){
            queryParams.put("psType",psType);
        }
        if(enterpriseName != null && !"".equals(enterpriseName)){
            queryParams.put("enterpriseName",enterpriseName);
        }
        if(controlLevels != null){
            queryParams.put("controlLevels",controlLevels.split(","));
        }
        return enterpriseService.findEnterpriseList(queryParams);
    }

    /**
     * 废气排放企业列表
     * @return
     */
    @RequestMapping(value = "wastegas/enterpriselist")
    public String findWasteGasEnterpriseList(){
        return enterpriseService.findWasteGasEnterpriseList();
    }

    /**
     * 大气点位监察接口
     * @param startLon 空气站点经度
     * @param startLat 空气站点纬度
     * @param endLon 气象结束点经度
     * @param endLat 气象结束点纬度
     * @param distance 缓冲区距离
     * @param spatialDis 缓冲区空间距离
     * @return
     */
    @RequestMapping(value = "wastegas/bufferenterprises")
    public Object findBufferedEnterpriseList(
            @RequestParam(value = "startlon",required = true) Double startLon,
            @RequestParam(value = "startlat",required = true) Double startLat,
            @RequestParam(value = "endlon",required = false) Double endLon,
            @RequestParam(value = "endlat",required = false) Double endLat,
            @RequestParam(value = "distance",required = true) Double distance,
            @RequestParam(value = "spatialdis",required = true) Double spatialDis){
        return enterpriseService.findBufferedEnterprises(startLon,startLat,endLon,endLat,distance,spatialDis);
    }

    /**
     * 获取废水排放企业或污水厂企业
     * @param itemCode 监测项目编号
     * @param state 状态比较（gt：大于；lt：小于；ge：大于等于；le：小于等于）
     * @param value 比较值
     * @param riverName 河流名称
     * @param stationName 断面名称
     * @param controlLevels 控制级别编号
     * @return
     */
    @RequestMapping(value = "wastewater/enterpriselist")
    public String findWasteWaterEnterpriseList(
            @RequestParam(value = "item",required = false) String itemCode,
            @RequestParam(value = "state",required = false) String state,
            @RequestParam(value = "value",required = false) Double value,
            @RequestParam(value = "river",required = false) String riverName,
            @RequestParam(value = "station",required = false) String stationName,
            @RequestParam(value = "controllevels",required = false) String controlLevels
    ){
        Map<String,Object> queryParams = new HashMap<>();
        Map<String,Object> itemParams = null;
        if(riverName != null && !"".equals(riverName)){
            queryParams.put("riverName",riverName);
        }
        if(stationName != null && !"".equals(stationName)){
            queryParams.put("stationName",stationName);
        }
        if(controlLevels != null && !"".equals(controlLevels)){
            queryParams.put("controlLevels",controlLevels.split(","));
        }

        if(itemCode != null && !"".equals(itemCode)
                && state != null && !"".equals(state) && value != null){
            itemParams = new HashMap<>();
            itemParams.put("itemCode",itemCode);
            itemParams.put("state",state);
            itemParams.put("value",value);
        }
        return enterpriseService.findWasteWaterorPlantEnterpriseList(queryParams,itemParams);
    }

    /**
     * 污水厂企业列表
     * @return
     */
    @RequestMapping(value = "treatmentplant/enterpriselist")
    public String findTreatmentPlantEnterpriseList(){
        return enterpriseService.findTreatmentPlantEnterpriseList();
    }
}
